---
title: "Tunisia"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
theme: bootstrap
vertical_layout: scroll
html_document: default
output: html_document
---
```{r setup, include=FALSE}
library(flexdashboard)
library(stringi)
library(evaluator)
library(shiny)
library(leaflet)
```
About Commerce
=====================================
Column {.sidebar data-width=300}
-------------------------------------
`About Tunisian Trade`
Tunisia is the 74th largest export economy in the world. In 2016, Tunisia exported $15.3B and imported $19.5B, resulting in a negative trade balance of $4.22B. In 2016 the GDP of Tunisia was $42.1B and its GDP per capita was $11.6k.
Tunisia's main import and export partners are the European Union and China. Cross-border trade is threatened by the chaotic situation in Libya. France remains its first supplier and customer. Tunisia's main export goods are textiles and leather, mechanical and electrical products, food products and energy products. The country imports raw and semi-finished materials, equipment goods, consumption goods (other than food) and financial and insurance services.
`Trade Balance`
As of 2016 Tunisia had a negative trade balance of $4.22B in net imports. As compared to their trade balance in 1995 when they still had a negative trade balance of $1.95B in net imports.
```{r}
```
Row {.tabset .tabset-fade}
------------------------------------------------------------------------------
### Bubble Chart (Import/Export)
```{r}
library(readxl)
EXIM <- read_excel("~/Downloads/EXIM.xlsx")
library(plotly)
colors <- c('#4AC6B7', '#1972A4', '#965F8A', '#FF7070', '#C61951','#CCCC00')
EXIM$Exchange = EXIM$Export + EXIM$Import
gg <- ggplot(EXIM, aes(Export, Import, color = Continent)) +
geom_point(aes(size = Exchange, frame = Year)) + scale_size_continuous(range = c(6.5,10))+
scale_x_continuous()+theme(legend.position="bottom") + scale_y_continuous()+
scale_colour_manual(values=alpha(colors,0.4))
ggplotly(gg)
```
### Trade over the years
``````{r}
library(readxl)
XI<- read_excel("~/Downloads/Socio-économique.xlsx")
colnames(XI)[3] <- "EXP"
colnames(XI)[4] <- "IMP"
library(plotly)
a <- list(autotick = FALSE,
ticks = "outside",
tick0 = 0,
dtick = 1,
ticklen = 3,
tickwidth = 2,
tickcolor = toRGB("purple"))
p=plot_ly(XI, x = ~Période, y = ~IMP, type = 'scatter', mode='lines',line = list(color = '#DB7093', width = 3),name = 'Importation')%>%
layout(xaxis = a) %>%
add_trace(y = ~EXP, name = "Exportation", connectgaps = TRUE,line = list(color = '#6495ED' ))%>% layout(
xaxis = list(title = "Year"),
yaxis = list(title = " ", tickfont = list(size = 15))
)
p
```
Row {data-height=450}
------------------------------------------------------------------------------
### Importation 2017
```{r}
library(readxl)
EXIM <- read_excel("~/Downloads/EXIM.xlsx")
data = EXIM[EXIM[,1] == 2017,]
library(ggplot2)
library(plotly)
# Basic barplot
p<-ggplot(data=data, aes(x=Continent, y=Import)) +
geom_bar(stat="identity", fill = "#FF6666")
ggplotly(p)
```
### Exportation 2017
```{r fig.width=4.5, fig.height=6, echo=FALSE}
library(readxl)
EXIM <- read_excel("~/Downloads/EXIM.xlsx")
data = EXIM[EXIM[,1] == 2017,]
library(ggplot2)
library(plotly)
# Basic barplot
p<-ggplot(data=data, aes(x=Continent, y=Export)) +
geom_bar(stat="identity", fill = "#5F9EA0")
ggplotly(p)
```
Companies in Tunisia
=====================================
Column {.sidebar data-width=600}
-------------------------------------
### Cartography
```{r}
library(leaflet)
library(readxl)
idr_gouv <- read_excel("~/Documents/idr_gouv.xlsx")
library(raster)
tnMAP<- getData(name="GADM", country="TUN", level=1)
i=match(tnMAP$HASC_1,idr_gouv$HASC_1)
tnMAP=cbind.Spatial(tnMAP,idr_gouv[i,])
col<-colorRampPalette(c("gray","#ff0055"))
e<-col(24)
pal<-colorNumeric(e,domain=tnMAP@data$IDR,n=24)
a=leaflet(tnMAP) %>%addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = .8,
opacity = 1,dashArray = "3",
fillColor = ~pal(IDR),
highlight = highlightOptions(
weight = 5,
color = "black",
bringToFront = TRUE))
labels<-sprintf("%s
%g",tnMAP@data$NAME_1,tnMAP@data$IDR)
labels<-labels%>%lapply(htmltools::HTML)
m<-leaflet(tnMAP) %>%addProviderTiles(providers$CartoDB.Positron)%>%addPolygons(fillColor=~pal(IDR),group="IDR",fillOpacity=10,col="black",weight=1.1,opacity=0.7,highlight=highlightOptions(weight=4.0,color="#FFFFFF",fillOpacity = 0.7,bringToFront = TRUE),label=labels,labelOptions=labelOptions( style = list("font-weight" = "normal", padding = "3px 8px"), textsize = "15px", direction = "auto"))
m<-m%>% addLegend(pal=pal,values=~IDR,opacity=1.5,position="bottomright",title = "")
rr<-"Evolution of private companies - 2016"
m<-m%>% addControl(rr, position = "bottomleft")
m
```
Row {data-height=600}
-------------------------------------
### Boxplot
```{r}
library(plotly)
library(readxl)
library(readxl)
Ty <- read_excel("~/Downloads/Ty.xlsx")
p <- ggplot(Ty, aes(Type, Tunisia, fill = Type)) +
geom_boxplot(size = 1) +
geom_boxplot(outlier.shape = NA) +
ggtitle("Distribution of Tunisian/Foreign compagnies in Tunisia (1996-2016)") +
theme(axis.title.x=element_blank(),
axis.ticks.x=element_blank(),axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())
p<-p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Need to modify the plotly object to make sure line width is larger than default
p <- plotly_build(p)
p
```